package cn.com.headfree.feedback.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gta.edu.sdk.base.service.impl.BaseServiceImpl;
import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.entity.TFeedBack;
import cn.com.headfree.entity.TSysDictionaryInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.feedback.dao.IFeedbackDao;
import cn.com.headfree.feedback.service.IFeedbackService;

/**  
 * @Title  FeedBackServiceImpl.java   
 * @Package cn.com.headfree.feedback.service.impl   
 * @Description 意见反馈serviceImpl
 * @author yangwang 
 * @date  2020年8月3日 下午2:46:27   
 */
@Service("feedbackService")
public class FeedbackServiceImpl extends BaseServiceImpl<TFeedBack> implements IFeedbackService {
	private static final Logger LOG = Logger.getLogger(FeedbackServiceImpl.class);
	
	@Autowired
	private IFeedbackDao feedbackDao;
	
	/**
	 * 保存意见反馈
	 * @param user
	 * @param feeback 意见信息
	 *  */
	@Transactional
	@Override
	public void saveFeedback(User user,TFeedBack feeback) {
		TFeedBack old = feedbackDao.getFeedback(feeback.getId());
		Date date = new Date();
		if((null != old)&&(StringUtils.isNotBlank(old.getId()))) {//更新
			feeback.setUpdaterId(user.getFax());
			feeback.setUpdaterName(user.getName());
			feeback.setUpdateTime(date);
			feeback.setFeedbackTime(date);
			feeback.setStatus(1);
			feedbackDao.updateFeedback(feeback);
		}else {//新增
			feeback.setCreaterId(user.getFax());
			feeback.setCreaterName(user.getName());
			feeback.setCreateTime(date);
			feeback.setUpdaterId(user.getFax());
			feeback.setUpdaterName(user.getName());
			feeback.setUpdateTime(date);
			feeback.setOpinionTime(date);
			feeback.setStatus(0);
			feedbackDao.insertFeedback(feeback);
		}
	}
	
	/**
	 * 删除意见反馈
	 * @param user
	 * @param ids 多个id以逗号分隔
	 *  */
	@Transactional
	@Override
	public void deleteFeedback(User user,String ids) {
		feedbackDao.deleteFeedback(user, ids);
	}
	
	/**
	 * 获取意见反馈
	 * @param id 主键
	 * @return TFeedBack
	 *  */
	@Override
	public TFeedBack getFeedback(String id) {
		TFeedBack feedback = feedbackDao.getFeedback(id);
		if((null != feedback)&&(StringUtils.isNotBlank(feedback.getId()))) {
			List<TSysDictionaryInfo> dictList = feedbackDao.getDictionarys("feedback_statu");
			if((null != dictList)&&(0 < dictList.size())) {
				Optional<TSysDictionaryInfo> opt = dictList.stream().filter(d -> ((null !=d)&&(StringUtils.isNotBlank(d.getValue()))&&(StringUtils.equals(d.getValue(), String.valueOf(feedback.getStatus()))))).findFirst();
				if(opt.isPresent()) {
					feedback.setStatusName(opt.get().getName());
				}
			}
		}
		
		return feedback;
	}
	
	/**
	 * 获取意见反馈分页信息
	 * @param user
	 * @param page 分页
	 * @param feeback 
	 *  */
	@Override
	public void getPage(User user,Page<TFeedBack> page,TFeedBack feeback) {
		//判断是否为管理员
		if(!(StringUtils.equals("2", user.getRemark()) || StringUtils.equals("3", user.getRemark()))){//管理员
			feeback.setCreaterId(user.getFax());
		}
		//获取分页数据
		feedbackDao.getPage(page, feeback);
		if((null != page)&&(null != page.getResult())&&(0 < page.getResult().size())){
			List<TSysDictionaryInfo> dictList = feedbackDao.getDictionarys("feedback_statu");
			if((null != dictList)&&(0 < dictList.size())) {
				for(TFeedBack fb:page.getResult()) {
					if((null != fb)&&(StringUtils.isNotBlank(fb.getId()))&&(null != fb.getStatus())) {
						Optional<TSysDictionaryInfo> opt = dictList.stream().filter(d -> ((null !=d)&&(StringUtils.isNotBlank(d.getValue()))&&(StringUtils.equals(d.getValue(), String.valueOf(fb.getStatus()))))).findFirst();
						if(opt.isPresent()) {
							fb.setStatusName(opt.get().getName());
						}
					}
				}
			}
		}
	}
	
	/**
	 * 获取字典列表
	 * @param value
	 * @return List<TSysDictionaryInfo>
	 *  */
	@Override
	public List<TSysDictionaryInfo> getDictionarys(String value){
		return feedbackDao.getDictionarys(value);
	}
	
}
